// ActionScript file
import mx.collections.XMLListCollection;
import mx.rpc.events.ResultEvent;

	
	private var dateForMsg:String;
	private var timeForMsg:String;
	private var writedDay:String;
	private var msgMode:String;
	
	[Bindable] private var writeToSid:String;
	[Bindable] private var writeToDname:String;
	[Bindable] private var encodeWriteToDname:String;
	[Bindable] private var dayForMsg:String;
	[Bindable] private var encodeMsg:String;
	[Bindable] private var deleteToSid:String;
	[Bindable] private var deleteFromSid:String;
	//[Bindable] private var encodeDeleteToDname:String;
	//[Bindable] private var encodeDeleteFromDname:String;
	[Bindable] private var dayForDeleteMsg:String;
	
	private var selectedMsg:XMLListCollection = new XMLListCollection();
	private var dataset_todayForMsg:XMLListCollection = new XMLListCollection();
	[Bindable] private var dataset_message:XMLListCollection = new XMLListCollection();
	
	private function checkSendMsg(mode:String):void{
		if(mode=="F"){
			writeToSid = selectedFSid;
			writeToDname = friendsList[loadedMfIndex][3].toString();
			lab_writeToDname.text = writeToDname;
			reqCheckTimeForMsg.send();
		}else if(mode=="P"){
			writeToSid = selectedPSid;
			writeToDname = pDName;
			lab_writeToDname.text = writeToDname;
			reqCheckTimeForMsg.send();
		}
	}
	
	private function checkTimeForMsg(event:ResultEvent):void{
		dataset_todayForMsg.removeAll();
		dataset_todayForMsg = new XMLListCollection(new XMLList(event.result));
		dateForMsg = dataset_today.child("date").text().toString();
		timeForMsg = dataset_today.child("time").text().toString();
		
		lab_todayForMsg.text = dateForMsg.substring(0,4)+"년 "+dateForMsg.substring(4,6)+"월 "+dateForMsg.substring(6)+"일   "+timeForMsg.substring(0,2)+"시 "+timeForMsg.substring(2,4)+"분 ";
	}
	
	private function loadMsg():void{
		msgMode = "load";
		cancelWritedMsg();
		cancelReadedMsg();
		reqLoadMsg.send();
	}
	
	private function confirmMsg():void{
		msgMode = "confirm";
		cancelWritedMsg();
		cancelReadedMsg();
		reqConfirmMsg.send();
	}
	
	private function sendWritedMsg():void{
		if(Boolean(lab_writeToDname.text) && Boolean(area_writeMsg.text) && Boolean(writeToSid)){
			encodeWriteToDname = escape(writeToDname);
			encodeDName = escape(displayName);
			dayForMsg = dateForMsg+timeForMsg;
			encodeMsg = escape(area_writeMsg.text);
			
			reqSendMsg.send();
		}
		lab_msgNotibar.text = "받을 사람과 메시지 내용이 올바른지 확인해주세요."
	}
	
	private function cancelWritedMsg():void{
		writeToSid = "";
		writeToDname = "";
		lab_writeToDname.text = writeToDname;
		lab_todayForMsg.text = "";
		area_writeMsg.text = "";
		lab_msgNotibar.text = "메시지 작성을 취소했습니다.";
	}
	
	private function cancelReadedMsg():void{
		lab_readFromDname.text = "";
		lab_writedDay.text = "";
		area_readMsg.text = "";
	}
	
	private function sendedMsg():void{
		cancelWritedMsg();
		lab_msgNotibar.text = "메시지를 전송했습니다.";
	}
	

	private function loadedMsg(event:ResultEvent):void{
		btn_deleteAllMsg.enabled = true;
		btn_replyMsg.enabled = true;
		btn_deleteSelectedMsg.enabled = true;
		btn_sendToMe.enabled = true;
		btn_sendWritedMsg.enabled = true;
		btn_cancelWritedMsg.enabled = true;
		lab_orFromTo.text = "From : ";
		
		var dataset_rawMessage:XMLListCollection = new XMLListCollection;
		dataset_rawMessage = new XMLListCollection(new XMLList(event.result.message));
		dataset_message = new XMLListCollection;
		dataset_message = dataset_rawMessage;
		
		for(var i:int=0; i<=(dataset_rawMessage.length-1); i++){
			dataset_message.getItemAt(i).from = unescape(dataset_message.getItemAt(i).child("from"));
			dataset_message.getItemAt(i).to = unescape(dataset_message.getItemAt(i).child("to"));
			dataset_message.getItemAt(i).msg = unescape(dataset_message.getItemAt(i).child("msg"));
		}
		
		dgrid_message.dataProvider = null;
		dgrid_message.dataProvider = dataset_message;
		lab_msgNotibar.text = "받은 쪽지들을 불러왔습니다.";
		lab_changeBoxNoti.text = "";
	}
	
	private function confirmedMsg(event:ResultEvent):void{
		btn_deleteAllMsg.enabled = false;
		btn_replyMsg.enabled = false;
		btn_deleteSelectedMsg.enabled = false;
		btn_sendToMe.enabled = false;
		btn_sendWritedMsg.enabled = false;
		btn_cancelWritedMsg.enabled = false;
		lab_orFromTo.text = "To : ";
		
		var dataset_rawMessage:XMLListCollection = new XMLListCollection;
		dataset_rawMessage = new XMLListCollection(new XMLList(event.result.message));
		dataset_message = new XMLListCollection;
		dataset_message = dataset_rawMessage;
		
		for(var i:int=0; i<=(dataset_rawMessage.length-1); i++){
			dataset_message.getItemAt(i).from = unescape(dataset_message.getItemAt(i).child("from"));
			dataset_message.getItemAt(i).to = unescape(dataset_message.getItemAt(i).child("to"));
			dataset_message.getItemAt(i).msg = unescape(dataset_message.getItemAt(i).child("msg"));
		}
		
		dgrid_message.dataProvider = null;
		dgrid_message.dataProvider = dataset_message;
		lab_msgNotibar.text = "보낸 쪽지들을 확인합니다.";
		lab_changeBoxNoti.text = "새메시지는 받은쪽지함에서 작성하세요.";
	}
	
	private function sendToMe():void{
		writeToSid = sid;
		writeToDname = displayName;
		lab_writeToDname.text = writeToDname;
		reqCheckTimeForMsg.send();
	}
	
	
	private function readMsg():void{
		selectedMsg = new XMLListCollection(new XMLList(dgrid_message.selectedItem.valueOf()));
		
		var enDay:String = dataset_message.getItemAt(dgrid_message.selectedIndex).child("day");
		var deDay:String = enDay.substring(0,4)+"년 "+enDay.substring(4,6)+"월 "+enDay.substring(6,8)+"일   "+enDay.substring(8,10)+"시 "+enDay.substring(10,12)+"분 ";
		
		writedDay = enDay;
		
		if(msgMode == "load"){
			lab_readFromDname.text = selectedMsg.child("from").toString();
		}else if(msgMode == "confirm"){
			lab_readFromDname.text = selectedMsg.child("to").toString();
		}
		lab_writedDay.text = deDay;
		area_readMsg.text = selectedMsg.child("msg").toString();
	}
	
	private function deleteSelectedMsg():void{
		deleteToSid = selectedMsg.child("toSid").toString();
		deleteFromSid = selectedMsg.child("fromSid").toString();
		dayForDeleteMsg = writedDay;
		
		reqDeleteMsg.send();
	}
	
	private function deletedMsg():void{
		lab_readFromDname.text = "";
		lab_writedDay.text = "";
		area_readMsg.text = "";
		
		dataset_message.removeAll();
		loadMsg();
	}
	
	private function replyMsg():void{
		writeToSid = selectedMsg.child("fromSid").toString();
		writeToDname = lab_readFromDname.text;
		lab_writeToDname.text = writeToDname;
		reqCheckTimeForMsg.send();
	}
	
	private function deleteAllMsg():void{
		reqDeleteAllMsg.send();
	}
	
	private function deletedAllMsg():void{
		lab_readFromDname.text = "";
		lab_writedDay.text = "";
		area_readMsg.text = "";
		
		dataset_message.removeAll()
		loadMsg();
	}
	
	